<?php

namespace Jobs\GeoLite2;

use Geo\City;
use Geo\Country;
use GeoLite2\Location;

class LocationsImportAction extends \Action {
	protected function run() {
		$this->response->noRender ( true );
		$this->response->noCache ( true );
		
		$limit = 10000000;
		$i = 0;
		
		$handle = fopen ( "GeoLite2-City-Locations.csv", "r" );
		if ($handle) {
			while ( $limit -- && (($data = fgetcsv($handle, 8192 )) !== false) ) {
				if ($i++ > 0) {
					//geoname_id	continent_code	continent_name	country_iso_code	country_name	subdivision_iso_code	subdivision_name	city_name	metro_code	time_zone
					
					$entity = new Location();
					
					$entity->geoname_id = intval($data[0]);
					$entity->continent_code = $data[1];
					$entity->continent_name = $data[2];
					$entity->country_iso_code = $data[3];
					$entity->country_name = $data[4];
					$entity->subdivision_iso_code = $data[5];
					$entity->subdivision_name = $data[6];
					$entity->city_name = $data[7];
					$entity->metro_code = $data[8];
					$entity->time_zone = $data[9];
					
					$entity->save();
					
					echo "#$i - ".$entity->city_name."\n";
					ob_flush();
				}
			}			
			fclose ( $handle );
		}
	}
}
